﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace LdsDownloader
{
    delegate void ShowExceptionBox(string message, string source);
    delegate void showStatus(string message, int current, int total);

    public partial class Form1 : Form
    {
        private Thread t;

        public Form1()
        {
            InitializeComponent();
            XmlParser.showExcept = ShowException;
            XmlParser.showProgress = showProgress;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (btnGo.Text.Equals("Go"))
            {
                XmlParser.Path = "books";
                //XmlParser.createMimetype();
                XmlParser.createTOC("The title of this new book");
                //XmlParser.createOPF();
                //XmlParser.createContainer();
                //XmlParser.writeToTOC();
                txtBoxResultCount.Clear();
                richTextBox1.Clear();

                if (!string.IsNullOrEmpty(txtBoxAddress.Text) && txtBoxAddress.Text.Contains('.'))
                {
                    t = new Thread(() => XmlParser.processPage(txtBoxAddress.Text));
                    t.IsBackground = true;
                    btnGo.Text = "Stop";
                    t.Start();
                    ////txtBoxMain.AppendText(info);
                    //richTextBox1.AppendText(info);
                    //txtBoxResultCount.Text = count.ToString();
                }
            }
            else
            {
                if (t.IsAlive)
                {
                    t.Abort();
                    richTextBox1.AppendText("Cancelling...");
                    t.Join(10000);
                }
                if (!t.IsAlive)
                {
                    btnGo.Text = "Go";
                    richTextBox1.AppendText("Cancelled!");
                    progressBar1.Value = 0;
                }
            }
        }

        public void ShowException(string message, string source)
        {
            Invoke(new Action(
                    delegate()
                    {
                        ExceptionForm exception1 = new ExceptionForm();
                        exception1.showException(message, source);
                        exception1.Show();
                    }));
        }

        public void showProgress(string message, int current, int total)
        {
            int percent = (int) (((float)current / total) * 100);
            
            if (message.Equals("Done!"))
            {
                Invoke(new Action(
                    delegate(){
                        richTextBox1.AppendText(message);
                        btnGo.Text = "Go";
                    }));
            }
            else
            {
                Invoke(new Action(
                    delegate()
                    {
                        richTextBox1.AppendText("Processing page " + message + "\n");
                    }));
            }
            if (percent >= 0 && percent <= 100)
            {
                Invoke(new Action(
                    delegate()
                    {
                        progressBar1.Value = percent;
                        txtBoxProgress.Text = current.ToString() + "/" + total.ToString();
                    }));
            }
        }

        private void txtBoxQuery_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
